#include "common.h"

int main()
{
    //1.创造key值
    key_t key = ftok(PATHNAME,PROJ_ID);
    if(key < 0)
    {
        perror("ftok");
        return 1;
    }
    //2.创建共享内存
    int shmid = shmget(key,SIZE,IPC_CREAT | IPC_EXCL | 0666);
    if(shmid < 0)
    {
        perror("shmget");
        return 1;
    }
    //3.连接并死循环等待client连接
    char* mem = shmat(shmid,NULL,0);
    if(mem == (void*)-1)
    {
        perror("shmat");
        return 1;
    }
    while(1)
    {
        //不断从共享内存读取数据
        printf("client say: %s\n",mem);
        sleep(1);
    }
    //4.解除连接并释放共享内存
    int ret = shmdt(mem);
    if(ret < 0)
    {
        perror("shmdt");
        return 1;
    }
    shmctl(shmid,IPC_RMID,NULL);
    return 0;
}